package com.kaiwukj.android.ufamily.mvp.model;

import com.alibaba.fastjson.JSON;
import com.kaiwukj.android.mcas.integration.IRepositoryManager;
import com.kaiwukj.android.mcas.mvp.BaseModel;
import com.kaiwukj.android.ufamily.mvp.http.api.service.AccountService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.CommonService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.CommunityService;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.UserInfoParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.LoginResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.UserResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.account.AccountContract;

import javax.inject.Inject;

import io.reactivex.Observable;

public class AccountModel extends BaseModel implements AccountContract.Model {
    @Inject
    public AccountModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public Observable<Integer> updatePass(String newPass) {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .updatePwd(newPass)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> sendVerifyCode(String mobile) {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .sendVerifyCode(mobile)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<LoginResult> loginByPass(String username, String password) {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .loginByPass(username, password).compose(RxUtils.handleResult());
    }

    @Override
    public Observable<LoginResult> loginByCode(String mobile, String code) {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .loginByCode(mobile, code, "verifycode")
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<LoginResult> loginByWechat(String openId, String mobile, String code) {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .loginByWechat(openId, mobile, code, "wechat")
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<LoginResult> loginByFast(String token, String opToken, String operator) {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .loginByFast(token, opToken, operator, "onekey")
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<UserResult> getUserInfo() {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .getUserInfo()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<CommunityResult>> fetchNearCommunity(double lat, double lon) {
        return mRepositoryManager.obtainRetrofitService(CommunityService.class).
                fetchCommunity(lat, lon, 50)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<CommunityResult>> fetchCommunityByKeyword(String keyword) {
        return mRepositoryManager.obtainRetrofitService(CommunityService.class)
                .fetchCommunityByKeyword(keyword, 50)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> updateCommunity(int communityId) {
        return mRepositoryManager.obtainRetrofitService(CommunityService.class)
                .updateCommunity(communityId)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> updateUserInfo(UserInfoParams params) {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .updateUserInfo(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<String> getQNToken() {
        return mRepositoryManager.obtainRetrofitService(CommonService.class)
                .getQiNiuToken()
                .compose(RxUtils.handleResult());
    }
}
